package com.conversationboard.view;

import java.util.List;

import com.conversationboard.batch.jobs.stats.StatsOutput;
import com.conversationboard.batch.jobs.stats.StatsOutput.StatsBoard;
import com.conversationboard.batch.jobs.stats.StatsOutput.MostPopularThread;
import com.conversationboard.batch.jobs.stats.StatsOutput.Person;
import com.conversationboard.batch.jobs.stats.StatsOutput.PostsInMonth;
import com.conversationboard.batch.jobs.stats.StatsOutput.ThreadStarter;
import com.conversationboard.config.Configuration;

public class StatsDisplay {

    public static String displayMostPopularThreads(StatsOutput statsOutput) {

        List<MostPopularThread> threads = statsOutput.getMostPopularThreads();
        StringBuilder buffer = new StringBuilder();

        buffer.append("<p>Most Popular Threads Last Week</p>");

        buffer.append("<table class='table'>");

        for (MostPopularThread thread : threads) {
            buffer.append("<tr class='row'><td class='cell'>");
            buffer.append("<a href=\"" + Configuration.getInstance().getRoot() + "/ThreadSelectedControllerServlet?boardid=" + thread.getBoardId() + "&amp;threadid=" + thread.getThreadId() + "\">"
                    + thread.getTitle() + "</a>");
            buffer.append("</td><td class='cell'>");
            buffer.append(thread.getPosts());
            buffer.append("</td></tr>");
        }

        buffer.append("</table>");

        return buffer.toString();
    }


    public static String displayThreadStarters(StatsOutput statsOutput) {

        List<ThreadStarter> threadStarters = statsOutput.getThreadStarters();
        StringBuilder buffer = new StringBuilder();

        buffer.append("<p>Top Threads Starters</p>");

        buffer.append("<table class='table'>");

        for (ThreadStarter threadStarter : threadStarters) {
            buffer.append("<tr class='row'><td class='cell'>");
            buffer.append(threadStarter.getName());
            buffer.append("</td><td class='cell'>");
            buffer.append(threadStarter.getThreads());
            buffer.append("</td></tr>");
        }

        buffer.append("</table>");

        return buffer.toString();
    }


    public static String displayThreadsPerBoard(StatsOutput statsOutput) {

        List<StatsBoard> boards = statsOutput.getThreadsPerBoard();
        StringBuilder buffer = new StringBuilder();

        buffer.append("<p>Threads Per Board</p>");

        buffer.append("<table class='table'>");

        for (StatsBoard board : boards) {
            buffer.append("<tr class='row'><td class='cell'>");
            buffer.append(board.getBoardName());
            buffer.append("</td><td class='cell'>");
            buffer.append(board.getThreads());
            buffer.append("</td></tr>");
        }

        buffer.append("</table>");

        return buffer.toString();
    }


    public static String displayTopPostersOfAllTime(StatsOutput statsOutput) {

        List<Person> topPosters = statsOutput.getTopPostersOfAllTime();
        StringBuilder buffer = new StringBuilder();

        buffer.append("<p>Top Posters of All Time</p>");

        buffer.append("<table class='table'>");

        for (Person person : topPosters) {
            buffer.append("<tr class='row'><td class='cell'>");
            buffer.append(person.getName());
            buffer.append("</td><td class='cell'>");
            buffer.append(person.getPosts());
            buffer.append("</td></tr>");
        }

        buffer.append("</table>");

        return buffer.toString();

    }


    public static String displayTopPostersThisMonth(StatsOutput statsOutput) {

        List<Person> topPosters = statsOutput.getTopPostersThisMonth();
        StringBuilder buffer = new StringBuilder();

        buffer.append("<p>Top Posters So Far This Month</p>");

        buffer.append("<table class='table'>");

        for (Person person : topPosters) {
            buffer.append("<tr class='row'><td class='cell'>");
            buffer.append(person.getName());
            buffer.append("</td><td class='cell'>");
            buffer.append(person.getPosts());
            buffer.append("</td></tr>");
        }

        buffer.append("</table>");

        return buffer.toString();

    }


    public static String displayPostsPerMonth(StatsOutput statsOutput) {

        List<PostsInMonth> postsInMonth = statsOutput.getPostsPerMonth();
        StringBuilder buffer = new StringBuilder();

        buffer.append("<p>Number of Posts Per Month Since System Started</p>");

        buffer.append("<table class='table'>");

        for (PostsInMonth posts : postsInMonth) {
            buffer.append("<tr class='row'><td class='cell'>");
            buffer.append(posts.getYear() + "/" + posts.getMonth());
            buffer.append("</td><td class='cell'>");
            buffer.append(posts.getPosts());
            buffer.append("</td></tr>");
        }

        buffer.append("</table>");

        return buffer.toString();

    }


    public static String displayProcessingTime(StatsOutput statsOutput) {

        long elapsedTime = statsOutput.getSecondsToProcess();
        return elapsedTime + " seconds";
    }

}
